package com.yaorange.nongjt.service.impl;

import java.util.List;

import com.yaorange.common.exception.ServiceException;
import com.yaorange.nongjt.mapper.NjtUserRegisterMapper;
import com.yaorange.common.utils.DateUtils;
import com.yaorange.common.utils.SecurityUtils;
import com.yaorange.nongjt.domain.NjtUserRegister;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yaorange.nongjt.mapper.NjtUserPerfectMapper;
import com.yaorange.nongjt.domain.NjtUserPerfect;
import com.yaorange.nongjt.service.INjtUserPerfectService;
import org.springframework.transaction.annotation.Transactional;


/**
 * 用户完善信息Service业务层处理
 *
 * @author junjiang
 * @date 2025-02-27
 */
@Service
public class NjtUserPerfectServiceImpl implements INjtUserPerfectService
{
    @Autowired
    private NjtUserPerfectMapper njtUserPerfectMapper;
    @Autowired
    private NjtUserRegisterMapper njtUserRegisterMapper;


    /**
     * 查询用户完善信息
     *
     * @param id 用户完善信息主键
     * @return 用户完善信息
     */
    @Override
    public NjtUserPerfect selectNjtUserPerfectById(Long id)
    {
        return njtUserPerfectMapper.selectNjtUserPerfectById(id);
    }

    /**
     * 查询用户完善信息列表
     *
     * @param njtUserPerfect 用户完善信息
     * @return 用户完善信息
     */
    @Override
    public List<NjtUserPerfect> selectNjtUserPerfectList(NjtUserPerfect njtUserPerfect)
    {
        return njtUserPerfectMapper.selectNjtUserPerfectList(njtUserPerfect);
    }

    /**
     * 新增用户完善信息
     *
     * @param njtUserPerfect 用户完善信息
     * @return 结果
     */
    @Override
    public int insertNjtUserPerfect(NjtUserPerfect njtUserPerfect)
    {
        njtUserPerfect.setCreateTime(DateUtils.getNowDate());
        return njtUserPerfectMapper.insertNjtUserPerfect(njtUserPerfect);
    }

    /**
     * 修改用户完善信息
     *
     * @param njtUserPerfect 用户完善信息
     * @return 结果
     */
    @Override
    public int updateNjtUserPerfect(NjtUserPerfect njtUserPerfect)
    {
        njtUserPerfect.setUpdateTime(DateUtils.getNowDate());
        return njtUserPerfectMapper.updateNjtUserPerfect(njtUserPerfect);
    }

    /**
     * 批量删除用户完善信息
     *
     * @param ids 需要删除的用户完善信息主键
     * @return 结果
     */
    @Override
    public int deleteNjtUserPerfectByIds(Long[] ids)
    {
        return njtUserPerfectMapper.deleteNjtUserPerfectByIds(ids);
    }

    /**
     * 删除用户完善信息信息
     *
     * @param id 用户完善信息主键
     * @return 结果
     */
    @Override
    public int deleteNjtUserPerfectById(Long id)
    {
        return njtUserPerfectMapper.deleteNjtUserPerfectById(id);
    }

    /**
     * @param njtUserPerfect 用户完善信息对象
     * @descripiton 完善用户信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void perfect(NjtUserPerfect njtUserPerfect) {
        try {
            // 填充用户完善信息时(更新和插入)需要的id，从当前操作的用户信息中获取并设置
            Long userId = SecurityUtils.getUserId();
            njtUserPerfect.setId(userId);
            // 更新用户注册信息表
            NjtUserRegister njtUserRegister = new NjtUserRegister();
            njtUserRegister.setId(userId);
            njtUserRegister.setPerfectTime(DateUtils.getNowDate());
            njtUserRegister.setPerfectFlag(1);
            njtUserRegister.setUpdateTime(njtUserRegister.getPerfectTime());
            int rows = njtUserRegisterMapper.updateNjtUserRegister(njtUserRegister);
            System.out.println(rows);
            // 新增用户完善信息表
            int row = njtUserPerfectMapper.insertNjtUserPerfect(njtUserPerfect);
            System.out.println(row);
        } catch (Exception e) {
            throw new ServiceException("完善信息提交失败！");
        }
    }

    /**
     * 根据⽤户注册id查询会员完善信息
     *
     * @param userId
     * @return
     */
    @Override
    public NjtUserPerfect getByRegisterUserId(Long userId) {
        NjtUserPerfect query = new NjtUserPerfect();
        query.setUserId(userId);
        List<NjtUserPerfect> njtUserPerfects = selectNjtUserPerfectList(query)
                ;
        if (null != njtUserPerfects && !njtUserPerfects.isEmpty()) {
            return njtUserPerfects.get(0);
        }
        return null;
    }
}
